package com.bookstore.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bookstore.Domain.Book;
import com.bookstore.Domain.Borrows;
import com.bookstore.Domain.User;
import com.bookstore.mapper.BookMapper;
import com.bookstore.Service.BookService;
import com.bookstore.mapper.BorrowsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 未来
 */
@Service
public class BookServiceImpl implements BookService {
    private final BookMapper bookMapper;
    private final BorrowsMapper borrowsMapper;

    @Autowired
    public BookServiceImpl(BookMapper bookMapper,BorrowsMapper borrowsMapper) {
        this.bookMapper = bookMapper;
        this.borrowsMapper = borrowsMapper;
    }

    @Override
    public List<Book> findAll() {
        return bookMapper.selectList(null);
    }

    @Override
    public Book findByName(String name) {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        return bookMapper.selectOne(wrapper);
    }

    @Override
    public boolean updateBook(Book book) {
        return  bookMapper.updateById(book) > 0;
    }

    @Override
    public boolean deleteBook(int id) {
        return bookMapper.deleteById(id)>0;
    }

    @Override
    public boolean saveBook(Book book) {
        return bookMapper.insert(book)>0;
    }

    @Override
    public List<Book> getPages(int page, int number) {
        Page<Book> pages = new Page<>(page,number);
        IPage<Book> IPage = bookMapper.selectPage(pages, null);
        return IPage.getRecords();
    }

    @Override
    public List<Book> getBook_time(String start_time, String end_time) {
        QueryWrapper<Book> wrapper = new QueryWrapper<>();
        wrapper.between("time", Timestamp.valueOf(start_time+"00:00:00"),Timestamp.valueOf(end_time+"00:00:00"));
        return bookMapper.selectList(wrapper);
    }

    @Override
    public boolean deleteBookAlist(int[] ids) {
        List<Integer> idList = Arrays.stream(ids).boxed().collect(Collectors.toList());
        return bookMapper.deleteBatchIds(idList)>0;
    }

    @Override
    public List<Book> searchBook(String query) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        // 设置模糊查询条件
        queryWrapper.like("name",query);
        return bookMapper.selectList(queryWrapper);
    }

    @Override
    public boolean borrows(Book book) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        // 设置模糊查询条件
        queryWrapper.eq("id",book.getId());
        Book book1 = bookMapper.selectOne(queryWrapper);
        //必须创建对象
        Borrows borrows = new Borrows();
        borrows.setBookAuthor(book1.getAuthor());
        borrows.setBookImage(book1.getImage());
        borrows.setBookName(book1.getName());
        borrows.setName(book.getName());
        return borrowsMapper.insert(borrows)>0;
    }

    @Override
    public List<Borrows> getAllborrow() {
        return borrowsMapper.selectList(null);
    }


}
